<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--
 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
 * Copyright (C) 2003-2007 Frederico Caldeira Knabben
 *
 * == BEGIN LICENSE ==
 *
 * Licensed under the terms of any of the following licenses at your
 * choice:
 *
 *  - GNU General Public License Version 2 or later (the "GPL")
 *    http://www.gnu.org/licenses/gpl.html
 *
 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 *    http://www.gnu.org/licenses/lgpl.html
 *
 *  - Mozilla Public License Version 1.1 or later (the "MPL")
 *    http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * == END LICENSE ==
 *
 * Link dialog window.
-->
<html>
<head>
	<title>Template Properties</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="robots" content="noindex, nofollow" />
	<script type="text/javascript">

var oEditor		= window.parent.InnerDialogLoaded() ;
var FCK			= oEditor.FCK ;
var FCKLang		= oEditor.FCKLang ;
var FCKConfig	= oEditor.FCKConfig ;
var FCKRegexLib	= oEditor.FCKRegexLib ;
var FCKTools	= oEditor.FCKTools ;

document.write( '<script src="' + FCKConfig.BasePath + 'dialog/common/fck_dialog_common.js" type="text/javascript"><\/script>' ) ;

	</script>
	<script type="text/javascript">

//#### Dialog Tabs

// Set the dialog tabs.
window.parent.AddTab( 'Edit', FCKLang.wikiTabEdit || 'Edit' ) ;
window.parent.AddTab( 'Manual', FCKLang.wikiTabManual || 'Manual' ) ;

function OnDialogTabChange( tabCode )
{
	ShowE('divEdit'		, ( tabCode == 'Edit' ) ) ;
	ShowE('divManual'		, ( tabCode == 'Manual' ) ) ;
}

// Get the selected flash embed (if available).
var oFakeImage = FCK.Selection.GetSelectedElement() ;
var oTemplateSpan ;

if ( oFakeImage )
{
	if ( oFakeImage.tagName == 'IMG' && oFakeImage.getAttribute('_fck_mw_template') )
		oTemplateSpan = FCK.GetRealElement( oFakeImage ) ;
	else
		oFakeImage = null ;
}

window.onload = function()
{
	// Translate the dialog box texts.
	oEditor.FCKLanguageManager.TranslatePage(document) ;

	// Load the selected link information (if any).
	LoadSelection() ;

	// Activate the "OK" button.
	window.parent.SetOkButton( true ) ;
	window.parent.SetAutoSize( true ) ;
	GetE('xTemplateRaw').focus();
}

function LoadSearchResults( result )
{
	var results = result.responseText.Trim().split( '\n' ) ;
	var select = GetE( 'xWikiResults' ) ;

	if ( results.length > 0 && !( results.length == 1 && results[0].length == 0 ) )
	{
		for ( var i = 0 ; i < results.length ; i++ )
			FCKTools.AddSelectOption( select, 'Template:' + results[i], results[i] ) ;
	}
}

function LoadSelection()
{
	// Make an Ajax search for the pages.
	var empty = '';
	oEditor.window.parent.sajax_request_type = 'GET' ;
	oEditor.window.parent.sajax_do_call( 'wfSajaxSearchTemplateFCKeditor',[empty], LoadSearchResults ) ;

	if ( !oTemplateSpan ) return ;

	var inputText = FCKTools.HTMLDecode(oTemplateSpan.innerHTML);
	inputText = FCKConfig.ProtectedSource.Revert(inputText, 0);		//#2509
	if (inputText.length>0 && inputText.indexOf('{{#')<0 && inputText.indexOf('{{:')<0 )
	{
		var templateName = inputText.substring(2,inputText.indexOf('fckLR'));
		if (inputText.indexOf('fckLR')<1)
			templateName = inputText.substring(2,inputText.indexOf('|'));
		if (inputText.indexOf('|')<1)
			templateName = inputText.substring(2,inputText.indexOf('}}'));

		SetUrl(templateName.charAt(0).toUpperCase() + templateName.substr(1));
	}
	GetE('xTemplateRaw').value = inputText.replace(/fckLR/g,'\r\n').replace( /&quot;/g, '"' );
}

function SetUrl(link)
{
	var urlTemplate = oEditor.window.parent.location.pathname + '?title=Template:' + link + '&printable=yes';
	SetAttribute(GetE('xTemplateManual'),'src',urlTemplate);
	GetE('xTemplateRaw').value = "{{" + link + "}}";
}

//#### The OK button was hit.
function Ok()
{
	if ( !oTemplateSpan )
	{
		oTemplateSpan = FCK.EditorDocument.createElement( 'SPAN' ) ;
		oTemplateSpan.className = 'fck_mw_template' ;
	}

	var protectedValue = FCKConfig.ProtectedSource.Protect(GetE('xTemplateRaw').value);
	var templateData = FCKTools.HTMLEncode(protectedValue.toString().Trim().replace(/(\r\n|\n)/g, 'fckLR')).replace( /"/g, '&quot;' ) ;

	if ( !( /^{{[\s\S]+}}$/.test( templateData ) ) )
	{
		alert( FCKLang.wikiTmplEmpty || 'Templates must start with {{ and end with }}. Please check it.' ) ;
		return false ;
	}

	oTemplateSpan.innerHTML = templateData ;

	if ( !oFakeImage )
	{
		oFakeImage	= oEditor.FCKDocumentProcessor_CreateFakeImage( 'FCK__MWTemplate', oTemplateSpan ) ;
		oFakeImage.setAttribute( '_fck_mw_template', 'true', 0 ) ;
		oFakeImage	= FCK.InsertElement( oFakeImage ) ;
	}

	//GetE('xTestSpan').innerHTML = GetE('xTemplateRaw').value.Trim().replace( /"/g, '&quot;' ).replace(/(\r\n|\n)/g, '___') ;
	return true ;
}

	</script>
</head>
<body style="overflow: hidden">
	<div id="divEdit">
		<table cellpadding="0" cellspacing="0" border="0" width="100%" height="100%">
			<tr>
				<td>
					<span fcklang="wikiTmpl">Template raw definition (from {{ to }})</span><br />
				</td>
			</tr>
			<tr>
				<td height="100%">
					<textarea id="xTemplateRaw" style="width: 100%; height: 100%; font-family: Monospace"
						cols="50" rows="10" wrap="off"></textarea>
				</td>
			</tr>
			<tr>
				<td>
					<span id="xTestSpan" class="fck_mw_template"></span>
				</td>
			</tr>
		</table>
	</div>
	<div id="divManual" style="display: none">
		<table cellpadding="0" cellspacing="0" border="0" width="100%" height="100%">
			<tr>
				<select id="xWikiResults" style="width: 100%;" onchange="SetUrl( this.value );">
				<option fcklang="wikiTmpsel" value="">&lt;Pick up a template manual here&gt;</option>
				</select>
			</tr>
			<tr>
				<td>
					<iframe id="xTemplateManual" width="100%" scrolling="yes" height="100%" src="" style="margin:5px;padding:0px;"></iframe>
				</td>
			</tr>
		</table>
	</div>
</body>
</html>
